home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / GameboyDev / GBDK / lib / output.sol < prev    next >
Text File  |  1999-03-29  |  5KB  |  397 lines

  1.     .include    "global.s"
  2.  
  3.     .globl    .copy_vram
  4.     .globl    .vbl
  5.     .globl    .lcd
  6.     .globl    .int_0x40
  7.     .globl    .int_0x48
  8.     .globl    .remove_int
  9.  
  10.     .MAXCURSPOSX    = 0x13    ; In tiles
  11.     .MAXCURSPOSY    = 0x11
  12.  
  13.     .SPACE    = 0x20
  14.     .BS    = 0x08
  15.     .CR    = 0x0A        ; Unix
  16. ;    .CR    = 0x0D        ; Dos
  17.  
  18.     .area    _HEADER (ABS)
  19.  
  20.     .org    .MODE_TABLE+4*.T_MODE
  21.     JP    .tmode
  22.  
  23.     .module Terminal
  24.  
  25.     .area    _BSS
  26.  
  27. .curx::                ; Cursor position
  28.     .ds    0x01
  29. .cury::
  30.     .ds    0x01
  31.  
  32.     .area    _CODE
  33.  
  34.     ;; Enter text mode
  35. .tmode::
  36.     DI            ; Disable interrupts
  37.  
  38.     ;; Turn the screen off
  39.     LDH    A,(.LCDC)
  40.     BIT    7,A
  41.     JR    Z,1$
  42.  
  43.     ;; Turn the screen off
  44.     CALL    .display_off
  45.  
  46.     ;; Remove any interrupts setup by the drawing routine
  47.     LD    BC,#.vbl
  48.     LD    HL,#.int_0x40
  49.     CALL    .remove_int
  50.     LD    BC,#.lcd
  51.     LD    HL,#.int_0x48
  52.     CALL    .remove_int
  53. 1$:
  54.  
  55.     CALL    .tmode_out
  56.  
  57.     ;; Turn the screen on
  58.     LDH    A,(.LCDC)
  59.     OR    #0b10000001    ; LCD        = On
  60.                 ; BG        = On
  61.     AND    #0b11100111    ; BG Chr    = 0x8800
  62.                 ; BG Bank    = 0x9800
  63.     LDH    (.LCDC),A
  64.  
  65.     EI            ; Enable interrupts
  66.  
  67.     RET
  68.  
  69.     ;; Text mode (out only)
  70. .tmode_out::
  71.  
  72.     XOR    A
  73.     LD    (.curx),A
  74.     LD    (.cury),A
  75.  
  76.     LD    BC,#.tp1    ; Move characters (font_a)
  77.     LD    HL,#0x8000
  78.     LD    DE,#.endtp1-.tp1
  79.     CALL    .copy_vram
  80.  
  81.     LD    BC,#.tp2    ; Move characters (font_b)
  82.     LD    HL,#0x8800
  83.     LD    DE,#.endtp2-.tp2
  84.     CALL    .copy_vram
  85.  
  86.     LD    BC,#.tp1    ; Move characters (font_a)
  87.     LD    HL,#0x9000
  88.     LD    DE,#.endtp1-.tp1
  89.     CALL    .copy_vram
  90.  
  91.     ;; Clear screen
  92.     CALL    .cls
  93.  
  94.     LD    A,#.T_MODE
  95.     LD    (.mode),A
  96.  
  97.     RET
  98.  
  99.     ;; Print a character without interpretation
  100. .out_char::
  101.     CALL    .set_char
  102.     CALL    .adv_curs
  103.     RET
  104.  
  105.     ;; Print a character with interpretation
  106. .put_char::
  107.     CP    #.CR
  108.     JR    NZ,1$
  109.     CALL    .cr_curs
  110.     RET
  111. 1$:
  112.     CALL    .set_char
  113.     CALL    .adv_curs
  114.     RET
  115.  
  116.     ;; Delete a character
  117. .del_char::
  118.     CALL    .rew_curs
  119.     LD    A,#.SPACE
  120.     CALL    .set_char
  121.     RET
  122.  
  123.     ;; Print the character in A
  124. .set_char:
  125.     PUSH    BC
  126.     PUSH    DE
  127.     PUSH    HL
  128.     LD    E,A
  129.  
  130.     LD    A,(.cury)    ; Y coordinate
  131.     LD    L,A
  132.     LD    H,#0x00
  133.     ADD    HL,HL
  134.     ADD    HL,HL
  135.     ADD    HL,HL
  136.     ADD    HL,HL
  137.     ADD    HL,HL
  138.     LD    A,(.curx)    ; X coordinate
  139.     LD    C,A
  140.     LD    B,#0x00
  141.     ADD    HL,BC
  142.     LD    BC,#0x9800
  143.     ADD    HL,BC
  144. 1$:
  145.     LDH    A,(.STAT)
  146.     AND    #0x02
  147.     JR    NZ,1$
  148.     LD    (HL),E
  149.     POP    HL
  150.     POP    DE
  151.     POP    BC
  152.     RET
  153.  
  154.     ;; Move the cursor left
  155. .l_curs:
  156.     LD    A,(.curx)    ; X coordinate
  157.     CP    #0
  158.     RET    Z
  159.     DEC    A
  160.     LD    (.curx),A
  161.     RET
  162.  
  163.     ;; Move the cursor right
  164. .r_curs:
  165.     LD    A,(.curx)    ; X coordinate
  166.     CP    #.MAXCURSPOSX
  167.     RET    Z
  168.     INC    A
  169.     LD    (.curx),A
  170.     RET
  171.  
  172.     ;; Move the cursor up
  173. .u_curs:
  174.     LD    A,(.cury)    ; Y coordinate
  175.     CP    #0
  176.     RET    Z
  177.     DEC    A
  178.     LD    (.cury),A
  179.     RET
  180.  
  181.     ;; Move the cursor down
  182. .d_curs:
  183.     LD    A,(.cury)    ; Y coordinate
  184.     CP    #.MAXCURSPOSY
  185.     RET    Z
  186.     INC    A
  187.     LD    (.cury),A
  188.     RET
  189.  
  190.     ;; Advance the cursor
  191. .adv_curs::
  192.     PUSH    HL
  193.     LD    HL,#.curx    ; X coordinate
  194.     LD    A,#.MAXCURSPOSX
  195.     CP    (HL)
  196.     JR    Z,1$
  197.     INC    (HL)
  198.     JR    99$
  199. 1$:
  200.     LD    (HL),#0x00
  201.     LD    HL,#.cury    ; Y coordinate
  202.     LD    A,#.MAXCURSPOSY
  203.     CP    (HL)
  204.     JR    Z,2$
  205.     INC    (HL)
  206.     JR    99$
  207. 2$:
  208.     ;; See if scrolling is disabled
  209.     LD    A,(.mode)
  210.     AND    #.M_NO_SCROLL
  211.     JR    Z,3$
  212.     ;; Nope - reset the cursor to (0,0)
  213.     XOR    A
  214.     LD    (.cury),A
  215.     LD    (.curx),A
  216.     JR    99$
  217. 3$:    
  218.     CALL    .scroll
  219. 99$:
  220.     POP    HL
  221.     RET
  222.  
  223.     ;; Rewind the cursor
  224. .rew_curs:
  225.     PUSH    HL
  226.     LD    HL,#.curx    ; X coordinate
  227.     XOR    A
  228.     CP    (HL)
  229.     JR    Z,1$
  230.     DEC    (HL)
  231.     JR    99$
  232. 1$:
  233.     LD    (HL),#.MAXCURSPOSX
  234.     LD    HL,#.cury    ; Y coordinate
  235.     XOR    A
  236.     CP    (HL)
  237.     JR    Z,99$
  238.     DEC    (HL)
  239. 99$:
  240.     POP    HL
  241.     RET
  242.  
  243.     ;; Advance the cursor to the next line
  244. .cr_curs::
  245.     PUSH    HL
  246.     XOR    A
  247.     LD    (.curx),A
  248.     LD    HL,#.cury    ; Y coordinate
  249.     LD    A,#.MAXCURSPOSY
  250.     CP    (HL)
  251.     JR    Z,2$
  252.     INC    (HL)
  253.     JR    99$
  254. 2$:
  255.     CALL    .scroll
  256. 99$:
  257.     POP    HL
  258.     RET
  259.  
  260.     ;; Scroll the whole screen
  261. .scroll:
  262.     PUSH    BC
  263.     PUSH    DE
  264.     PUSH    HL
  265.     LD    HL,#0x9800
  266.     LD    BC,#0x9800+0x20 ; BC = next line
  267.     LD    E,#0x20-0x01    ; E = height - 1
  268. 1$:
  269.     LD    D,#0x20        ; D = width
  270. 2$:
  271.     LDH    A,(.STAT)
  272.     AND    #0x02
  273.     JR    NZ,2$
  274.  
  275.     LD    A,(BC)
  276.     LD    (HL+),A
  277.     INC    BC
  278.     DEC    D
  279.     JR    NZ,2$
  280.     DEC    E
  281.     JR    NZ,1$
  282.  
  283.     LD    D,#0x20
  284. 3$:
  285.     LDH    A,(.STAT)
  286.     AND    #0x02
  287.     JR    NZ,3$
  288.  
  289.     LD    A,#.SPACE
  290.     LD    (HL+),A
  291.     DEC    D
  292.     JR    NZ,3$
  293.     POP    HL
  294.     POP    DE
  295.     POP    BC
  296.     RET
  297.  
  298.  
  299.     ;; Clear the whole screen
  300. .cls:
  301. _cls::
  302.     PUSH    DE
  303.     PUSH    HL
  304.     LD    HL,#0x9800
  305.     LD    E,#0x20        ; E = height
  306. 1$:
  307.     LD    D,#0x20        ; D = width
  308. 2$:
  309.     LDH    A,(.STAT)
  310.     AND    #0x02
  311.     JR    NZ,2$
  312.  
  313.     LD    (HL),#.SPACE
  314.     INC    HL
  315.     DEC    D
  316.     JR    NZ,2$
  317.     DEC    E
  318.     JR    NZ,1$
  319.     POP    HL
  320.     POP    DE
  321.     RET
  322.  
  323. _putchar::
  324.     LD    A,(.mode)
  325.     AND    #.T_MODE
  326.     JR    NZ,1$
  327.     PUSH    BC
  328.     CALL    .tmode
  329.     POP    BC
  330. 1$:
  331.     LDA    HL,2(SP)    ; Skip return address
  332.     LD    A,(HL)        ; A = c
  333.     CALL    .put_char
  334.     RET
  335.  
  336. _gotoxy::
  337.     LD    A,(.mode)
  338.     AND    #.T_MODE
  339.     JR    NZ,1$
  340.     PUSH    BC
  341.     CALL    .tmode
  342.     POP    BC
  343. 1$:
  344.     LDA    HL,2(SP)    ; Skip return address
  345.     LD    A,(HL+)        ; A = x
  346.     LD    (.curx),A
  347.     LD    A,(HL+)        ; A = y
  348.     LD    (.cury),A
  349.     RET
  350.  
  351. _posx::
  352.     LD    A,(.mode)
  353.     AND    #.T_MODE
  354.     JR    NZ,1$
  355.     PUSH    BC
  356.     CALL    .tmode
  357.     POP    BC
  358. 1$:
  359.     LD    A,(.curx)
  360.     LD    E,A
  361.     RET
  362.  
  363. _posy::
  364.     LD    A,(.mode)
  365.     AND    #.T_MODE
  366.     JR    NZ,1$
  367.     PUSH    BC
  368.     CALL    .tmode
  369.     POP    BC
  370. 1$:
  371.     LD    A,(.cury)
  372.     LD    E,A
  373.     RET
  374.  
  375. _setchar::
  376.     LD    A,(.mode)
  377.     AND    #.T_MODE
  378.     JR    NZ,1$
  379.     PUSH    BC
  380.     CALL    .tmode
  381.     POP    BC
  382. 1$:
  383.     LDA    HL,2(SP)    ; Skip return address
  384.     LD    A,(HL)        ; A = c
  385.     CALL    .set_char
  386.     RET
  387.  
  388.     .area    _DATA
  389.  
  390. .tp1:
  391.     .include    "font_a.h"
  392. .endtp1:
  393.  
  394. .tp2:
  395.     .include    "font_b.h"
  396. .endtp2:
  397.